var config = require('./config.js');

function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(formatNumber).join('/') +
        ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

/**
 * 获取系统信息,获取成功后:
 * 1.存入本地存储;
 * 2.存入App.globalData内存
 * @param successCb Function
 */
function getSystemInfo(successCb) {
    var app = getApp();
    var info = app.globalData.systemInfo;
    if (info) {
        typeof successCb == 'function' && successCb(info);
        return info;
    }
    wx.getSystemInfo({
        success: function (res) {
            wx.setStorage({
                key: config.storage.keySystemInfo,
                data: res
            });
            app.globalData.systemInfo = res;
            typeof successCb == 'function' && successCb(res);
        },
        fail: function (res) {
        },
        complete: function (res) {
        },
    });
}

/**
 * 获取本地存储的SessionId
 */
function getSessionId() {
    var sessionId;
    try {
        sessionId = wx.getStorageSync(config.storage.keySessionId);
    } catch (e) {
    }
    return sessionId;
}


/**
 * 初始化 And 存储 Categories.返回新的数组。
 * @param categoriesLi Array[String].
 * @return Array[{}]
 */
function initAndStorageCategories(categoriesLi) {
    let id = 0;
    let resultLi = [];
    categoriesLi && categoriesLi.forEach((item, index) => {
        resultLi.push({
            id: id++,
            name: item,
            selected: false,
            isTopic: false,
        });
    });
    wx.setStorage({
        key: config.storage.keyCategories,
        data: resultLi
    });
    return resultLi;
}

/**
 * 确保 ImageList 包含显示 mode。
 * mode 为 1,2,3,4,1,2,3....
 * 结果保存在传入的 resourceLi 参数中
 * @param resourceLi Array
 */
function addModeToList(resourceLi) {
    let mode = 1;
    resourceLi && resourceLi.forEach(item => {
        if (item && (item.list || item.resourceList)) {
            item.list = item.list || item.resourceList;
            item.resourceList = '';
            item.list && item.list.forEach(image => {
                image.url = image.smallPicUrl || image.picUrl;
            });
            if (mode > 4) {
                mode = 1;
            }
            item.mode = mode;
            mode++;
        }
    });
}


function miniImageObjSize (resourceLi) {
    return resourceLi.map(item => {
        let list = item.list;
        return {
            mode: item.mode,
            list: list.map(image => {
                return {
                    id: image.id,
                    backgroundColor: image.backgroundColor,
                    url: image.url
                }
            })
        }
    })
}

module.exports = {
    getSystemInfo: getSystemInfo,
    getSessionId: getSessionId,
    addModeToList: addModeToList
};
